<?php defined('BASEPATH') or exit('No direct script access allowed');

class Mybids_m extends MY_Model {
	var $task= "";
	function __construct(){
		parent::__construct();
		$this->load->library('module_helper');
		
	}
	
	function all_bids(){
		$MY_ACC = $this->user_entity->init('id', $_SESSION['user']['id']);
		
		$total = count( $this->db->where('account_code',$MY_ACC->getAccCode())->get(TBL_BID)->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : defaultNumberRecordsPerPageShowDataConfig();
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'buyer/my_bids/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where('account_code',$MY_ACC->getAccCode())->limit($records_per_page, $per_page )->get(TBL_BID)->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function active_bids(){
		$MY_ACC = $this->user_entity->init('id', $_SESSION['user']['id']);
		
		$total = count( $this->db->query("SELECT ".TBL_BID.".* FROM ".TBL_BID." INNER JOIN ".TBL_SHOW." ON ".TBL_BID.".show_ref_no = ".TBL_SHOW.".ref_no WHERE ".TBL_SHOW.".status='opening' AND ".TBL_BID.".account_code='".$MY_ACC->getAccCode()."'")
						->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : defaultNumberRecordsPerPageShowDataConfig();
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'buyer/my_bids/active_bids/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->select('BID.*')->from(TBL_BID." AS BID")->
						join( TBL_SHOW." AS SHOWTBL", "BID.show_ref_no=SHOWTBL.ref_no")->
						where( array( "BID.account_code"=>$MY_ACC->getAccCode(),"SHOWTBL.status"=>'opening' ) )->
						limit($records_per_page, $per_page )->
						get()->result();
				
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function awarded_bids(){
		$MY_ACC = $this->user_entity->init('id', $_SESSION['user']['id']);
		
		$total = count( $this->db->query("SELECT * FROM ".TBL_BID." INNER JOIN ".TBL_SHOW." ON ".TBL_BID.".show_ref_no = ".TBL_SHOW.".ref_no WHERE ".TBL_SHOW.".status='closed' AND ".TBL_BID.".account_code='".$MY_ACC->getAccCode()."' AND ".TBL_SHOW.".user_bid_won='".$MY_ACC->getId()."'" )
						->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : defaultNumberRecordsPerPageShowDataConfig();
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'buyer/my_bids/active_bids/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->select('BID.*')->from(TBL_BID." AS BID")->
						join( TBL_SHOW." AS SHOWTBL", "BID.show_ref_no=SHOWTBL.ref_no")->
						where( array( "BID.account_code"=>$MY_ACC->getAccCode(),"SHOWTBL.status"=>'closed' ,"SHOWTBL.user_bid_won"=>$MY_ACC->getId()) )->
						limit($records_per_page, $per_page )->
						get()->result();
				
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function search(){
		$MY_ACC = $this->user_entity->init('id', $_SESSION['user']['id']);
		
		$condition = array();
		if($_GET['title'] != "Title"){
			$title = mysql_real_escape_string(strtolower($_GET['title']));
			$condition[] = " LOWER(".TBL_SHOW.".name) LIKE '$title%' ";
		}
		
		$condition_str = implode('AND', $condition);
	    $condition_str = !empty($condition_str) ? $condition_str : '';
		
		$total = count( $this->db->query("SELECT ".TBL_BID.".* FROM ".TBL_BID." INNER JOIN ".TBL_SHOW." ON ".TBL_BID.".show_ref_no = ".TBL_SHOW.".ref_no WHERE  ".TBL_BID.".account_code='".$MY_ACC->getAccCode()."' AND $condition_str ")->result() ); 
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : 5;
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){  
			if($key != "per_page")
				$partial["$key"] = "$key=".urlencode($value);
		}
	 
		$pagination = create_pagination( 
								$uri = 'seller/search/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->query("SELECT ".TBL_BID.".* FROM ".TBL_BID." INNER JOIN ".TBL_SHOW." ON ".TBL_BID.".show_ref_no = ".TBL_SHOW.".ref_no WHERE  ".TBL_BID.".account_code='".$MY_ACC->getAccCode()."' AND $condition_str  LIMIT $per_page, $records_per_page")->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function delete_bid(){
		$ref_no = $_POST['ref_no'];
		$MY_ACC = $this->user_entity->init('id', $_SESSION['user']['id']);
		$SHOW = $this->show_entity->init('ref_no', $ref_no);
		
		$bid = $this->db->where(array('account_code'=>$MY_ACC->getAccCode(), 'show_ref_no'=>$SHOW->getRefNo()))->get(TBL_BID)->result();
		$this->db->where(array('account_code'=>$MY_ACC->getAccCode(), 'show_ref_no'=>$SHOW->getRefNo()))->delete(TBL_BID);
		$this->db->where(array('account_code'=>$MY_ACC->getAccCode(), 'show_ref_no'=>$SHOW->getRefNo()))->delete(TBL_BOOKING_FOR);
		//send email
		$this->crontab_email->cronUpdate("BIDCANCELBYBUYER_".$SHOW->getRefNo(),'_sendEmailToUser_buyerCancelBid', strval( $SHOW->getRefNo().",".$MY_ACC->getId().",".$bid[0]->id ), array(time()) );
		
		exit();
	}
	
}